package com.evan.review.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.evan.review.R;

/**
 * 全屏DialogFragment
 * 演示如何创建全屏对话框
 */
public class FullScreenDialogFragment extends DialogFragment {

    private EditText etInput;
    private FullScreenDialogListener listener;
    
    /**
     * 全屏对话框回调接口
     */
    public interface FullScreenDialogListener {
        void onFullScreenDialogSave(String result);
        void onFullScreenDialogCancel();
    }
    
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        
        // 确保Activity实现了接口
        if (context instanceof FullScreenDialogListener) {
            listener = (FullScreenDialogListener) context;
        } else {
            throw new ClassCastException(context.toString()
                    + " 必须实现FullScreenDialogListener接口");
        }
    }
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置全屏样式
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
    }
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fullscreen_dialog, container, false);
        
        etInput = view.findViewById(R.id.et_input);
        Button btnSave = view.findViewById(R.id.btn_save);
        Button btnCancel = view.findViewById(R.id.btn_cancel);
        
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onFullScreenDialogSave(etInput.getText().toString());
                }
                dismiss();
            }
        });
        
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onFullScreenDialogCancel();
                }
                dismiss();
            }
        });
        
        return view;
    }
    
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
    
    @Override
    public void onStart() {
        super.onStart();
        
        // 设置对话框宽高为全屏
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, 
                        WindowManager.LayoutParams.MATCH_PARENT);
            }
        }
    }
    
    @Override
    public void onCancel(@NonNull DialogInterface dialog) {
        super.onCancel(dialog);
        if (listener != null) {
            listener.onFullScreenDialogCancel();
        }
    }
    
    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }
} 